package algorithm;

import java.util.Stack;

/**
 * @ClassName Test
 * @Author 刘正星
 * @Date 2021/6/18 下午8:44
 * @Description
 */
public class KuohaoTest {


   static boolean match2(String input){
       int a =0;
       int b = 0;
       int c = 0;
       for (int i = 0; i < input.length() ; i++) {
           char c1 = input.charAt(i);
           if (c1=='('){
               a++;
           }
           if (c1=='{'){
               b++;
           }
           if (c1=='['){
               c++;
           }
           if (c1==')'){
               a--;
               if (a<0){
                   return false;
               }
           }

           if (c1=='}'){
               b--;
               if (b<0){
                   return false;
               }
           }

           if (c1==']'){
               c--;
               if (c<0){
                   return false;
               }
           }
       }

        return a==0&&b==0&&c==0;
    }



    static boolean match(String input){

        Stack<Character> strings = new Stack<>();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c=='('||c=='{'||c=='['){
                strings.push(c);
//                continue;

            }
            if (c==')'){
                if (strings.peek()!=null&&strings.peek()=='('){
                    strings.pop();
                }else {
                    return false;
                }

            }
            if (c=='}') {
                if (strings.peek() != null && strings.peek() == '{') {
                    strings.pop();
                }else {
                    return false;
                }
            }
            if (c==']'){
                if (strings.peek()!=null&&strings.peek()=='['){
                    strings.pop();
                }else {
                    return false;
                }
            }

        }
        return true;


    }



    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char cStack = stack.pop();
                boolean b1 = c == ')' && cStack != '(';
                boolean b2 = c == ']' && cStack != '[';
                boolean b3 = c == '}' && cStack != '{';
                if (b1 || b2 || b3) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }


    public static void main(String [] args){


        System.out.println(match2("[{{(())}}]"));

        System.out.println(match2("[{{(()}}"));

        System.out.println(match2(")({[]}"));

    }
}

